package apps.reading.spider;

import java.util.Date;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

import core.tool.common.StringHelper;
import core.tool.htmlparser.HtmlparserUtil;

import apps.reading.ReadingSpider;
import apps.reading.WebSourceParser;
import apps.reading.db.WebSourceBean;

@ReadingSpider(sourceType="2", type="1", url="http://www.iteye.com/blogs")
public class ItEyeBlogs extends BaseReading implements WebSourceParser {

	@Override
	WebSourceBean getWebSourceBeanOfStr(String result, String url) {
		WebSourceBean webSourceBean=new WebSourceBean();
		try {
			Parser parser = new Parser();
			parser.setInputHTML(result);
			NodeFilter filter = new TagNameFilter("A");
			NodeList nodelist = parser.parse(filter);
			for (int i = 0; i < nodelist.size(); i++) {
				Node node = nodelist.elementAt(i);
				LinkTag link = (LinkTag) nodelist.elementAt(i);
				if (i == 1) {
					webSourceBean.setContentURL(link.getAttribute("href"));
					webSourceBean.setTitle((link.getStringText()));
				}	
//				if (i == 7) {
//					webSourceBean.setAuthor((link.getStringText()));
//				}					
			}

//			filter = new TagNameFilter("DIV");
//			nodelist = parser.parse(filter);
//			for (int i = 0; i < nodelist.size(); i++) {
//				Node node = nodelist.elementAt(i);
//				if (i == 1) {
//					webSourceBean.setRemark(node.getText());
//					break;
//				}
//			}		
			
			
//			Node[] dateNodes=HtmlparserUtil.getNodeListOfResult(result, "span", "class", "date");
//			webSourceBean.setPublicDate(new Date(dateNodes[0].getText()));						
			

		} catch (Exception e) {
			e.printStackTrace();
		}
		return webSourceBean;
	}

	public WebSourceBean getContent(WebSourceBean webSourceBean) {
		String content=HtmlparserUtil.getNodeStr(webSourceBean.getContentURL(), "div", "class", "iteye-blog-content-contain",true);
		webSourceBean.setContent(content);
		return webSourceBean;
	}

	public List<WebSourceBean> getWebSourceTitleList(String url) {
		// TODO Auto-generated method stub
		return super.getWebSourceTitleList(url,"div", "class", "content");
	}
	
	
	public static void main(String[] args) {
		WebSourceParser webSourceParser=new ItEyeBlogs();
		ReadingSpider readingSpider=(ReadingSpider) webSourceParser.getClass().getAnnotations()[0];
		List<WebSourceBean> list=webSourceParser.getWebSourceTitleList(readingSpider.url());
		for(WebSourceBean webSourceBean:list) {
			WebSourceBean webSourceContent=webSourceParser.getContent(webSourceBean);
//			WebSourceBean webSourceContent=webSourceBean;
			webSourceContent.setId(StringHelper.getUUID());
			webSourceContent.setSourceType(readingSpider.sourceType());
			webSourceContent.setType(readingSpider.type());	
			System.out.println(webSourceBean);
		}			
		
		
		
	}

}
